package com.petcommunity.servlet;

import com.petcommunity.dao.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String type = request.getParameter("type");
        int id = Integer.parseInt(request.getParameter("id"));

        try {
            switch (type) {
                case "pet":
                    new PetDAO().deletePet(id);
                    response.sendRedirect("pets");
                    break;
                case "owner":
                    new OwnerDAO().deleteOwner(id);
                    response.sendRedirect("owners");
                    break;
                case "adoption":
                    new AdoptionDAO().deleteAdoption(id);
                    response.sendRedirect("adoptions");
                    break;
                case "lost":
                    new LostPetDAO().deleteLostPet(id);
                    response.sendRedirect("lostpets");
                    break;
                case "vaccine":
                    new VaccineRecordDAO().deleteVaccine(id);
                    response.sendRedirect("vaccines");
                    break;
                case "notice":
                    new NoticeDAO().deleteNotice(id);
                    response.sendRedirect("notices");
                    break;
                default:
                    response.sendError(HttpServletResponse.SC_BAD_REQUEST, "无效类型");
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("error.jsp");
        }
    }
}
